package com.fanfanwang.ffw.app.ui.my;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.adapter.GlyCarportManagementAdapter;
import com.fanfanwang.ffw.app.asynctask.DelParknumAsyncTask;
import com.fanfanwang.ffw.app.asynctask.GetParknumListAsyncTask;
import com.fanfanwang.ffw.app.base.BaseActivity;
import com.fanfanwang.ffw.app.bean.CarportBean;
import com.fanfanwang.ffw.app.bean.ParkNumBean;
import com.fanfanwang.ffw.app.common.StringUtils;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.fragment.GlyHomeFragment.IgetParknumList;
import com.fanfanwang.ffw.app.tenant.R;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.ui.HomeActivity;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;

/** 管理员 管理停车位，可以进行删除，跳转到添加界面 */
public class GlyMyCarportManagementAct extends BaseActivity {

	private CommonTitleBar commonTitleBar;
	private Activity mAct;
	private Intent mIntent;
	private ListView listview;
	private ArrayList<ParkNumBean> carportBeans;
	private GlyCarportManagementAdapter adapter;
	//删除停车位列表的回调
	public static IDeleteCarportable iDeleteCarportable;
	public int deletePos;//删除的停车位的pos
	//这个是得到停车位列表的回调函数
	public static IgetParknumList iGetParknumList;
	//这个是更新一下停车位列表，重新异步获取数据
	public static	IUpdateParknumList iUpdateParknumList;//这个是更新一下parknumlist,可以在添加了车位之后，调用
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initView();
		AppManager.getAppManager().addActivity(this);
		initData();
		setListener();
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.gly_activity_my_carport_management);
		mAct = this;
		commonTitleBar = (CommonTitleBar) findViewById(R.id.common_titlebar);
		listview=(ListView)findViewById(R.id.lv_carport);

		carportBeans=new ArrayList<ParkNumBean>();

	}
	@Override
	public void setListener() {
		commonTitleBar.setToolBarReturnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		commonTitleBar.setRightView(getString(R.string.add), new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mIntent=new Intent(GlyMyCarportManagementAct.this, GlyMyAddCarportAct.class);
				startActivity(mIntent);
			}
		});
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		commonTitleBar.setCenterText(getString(R.string.carport_management));
		commonTitleBar.setReturnVisible();
		setDeleteInterface();
		setInterface();
		//		getList();
		adapter=new GlyCarportManagementAdapter(mAct, carportBeans);
		new GetParknumListAsyncTask(mAct, SharedPreferencesUtil.getInstance(mAct).getParkingId()).execute("");
		listview.setAdapter(adapter);
	}
	public void setDeleteInterface(){
		iDeleteCarportable=new IDeleteCarportable() {

			@Override
			public void deleteCarport(int pos) {
				// TODO Auto-generated method stub
				/*carportBeans.remove(pos);
				adapter.resetData(carportBeans);
				adapter.notifyDataSetChanged();*/
				//第二个为停车位的id。不是车位号
				deletePos=pos;
				new DelParknumAsyncTask(mAct, carportBeans.get(pos).id).execute("");
			}

			@Override
			public void deleteSuccess() {
				// TODO 停车位删除成功
//				carportBeans.remove(deletePos);
//				adapter.resetData(carportBeans);
//				adapter.notifyDataSetChanged();
			}
		};

	}
	public interface IDeleteCarportable{
		public void deleteCarport(int pos);
		public void deleteSuccess();
	}
	public interface IAddCarportable{
		public void addCarport(ParkNumBean parkNumBean);
	}
	public interface IgetParknumList{//得到停车位的列表
		public void getParknumList(ArrayList<ParkNumBean> beans);
	}
	public void setInterface(){
		iGetParknumList=new IgetParknumList() {

			@Override
			public void getParknumList(ArrayList<ParkNumBean> beans) {
				// TODO Auto-generated method stub
				carportBeans.clear();
				carportBeans.addAll(beans);
				
				adapter.resetData(beans);
				adapter.notifyDataSetChanged();
			}
		};
		iUpdateParknumList=new IUpdateParknumList() {
			
			@Override
			public void updateParknumList() {
				// TODO Auto-generated method stub
				new GetParknumListAsyncTask(mAct,  SharedPreferencesUtil.getInstance(mAct).getParkingId()).execute("");
			}
		};
	}
	/**
	 * @author Administrator
	 *异步重新获取一下停车位列表
	 */
	public interface IUpdateParknumList{
		public void updateParknumList();
	}
}
